#!/bin/bash

if [ $# -ne 1 ] ; then
	echo "Uso: yarafay-xml-paths.sh file.xml"
	exit 1
fi

filename=$1
filename=${filename/.*}-local.xml

awk -v texturesPath=/share/apps/renders/textures/ '
	BEGIN { state = 0; }
	{ written = 0; }
	state == 0 && /\<texture name=\"/ { state = 2; }
	state == 2 && /\<filename[^_a-zA-Z0-9]/ { match($0, / sval="([^\/]*\/)+([^\/]+)"/, a); print "<filename sval=\"" texturesPath a[2] "\"/>"; written=1; }
	state == 2 && /<\/texture>/ { state = 0; }
	written == 0 { print; }
	written == 1 { written = 0; }
' $1 > $filename
